package ide.ccf.ccproject.common;

import java.util.Vector;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceProxy;
import org.eclipse.core.resources.IResourceProxyVisitor;
import org.eclipse.core.runtime.CoreException;


/**
 * CCF工程访问器。工具类，返回工程中符合指定扩展名的文件名列表。
 *
 */
public class ProjectVisitor implements IResourceProxyVisitor 
{
	@SuppressWarnings("rawtypes")
	private Vector result;
	private String extention;
	
	/**
	 * 构造器
	 */
	@SuppressWarnings("rawtypes")
	public ProjectVisitor() 
	{
		result = new Vector();
	}

	/**
	 * 访问
	 */
	@SuppressWarnings("unchecked")
	public boolean visit(IResourceProxy proxy) throws CoreException 
	{
		if (proxy.getName().endsWith(extention) && proxy.getType() == IResource.FILE) 
		{
			result.add(proxy.requestResource());
			return false;
		}
		return true;
	}
	
	/**
	 * 查找符合扩展名的文件
	 * @param project
	 * @param ext
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public Vector findFiles(IProject project, String ext) 
	{
		extention = "." + ext;
		
		try 
		{
			result.clear();
			project.accept(this, IResource.NONE);
			return result;
		} 
		catch (Exception e) 
		{
			return null;
		}
	}
}